<template>
    <div class="reg-wrap">
        <div class="ms-reg">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item v-if="this.is_phone == true"><el-button @click="get_code">获取验证码</el-button></el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input type="text" v-model="ruleForm.code" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                    <el-button type="text" @click="login">已有帐号？点击登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { Register, code } from '../../api/api.js';
export default {
    name: 'login',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入用户名'));
            } else {
                if (this.ruleForm.password !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        var validatePass3 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入手机号'));
            } else {
                if (!/^1[3456789]\d{9}$/.test(value)) {
                    callback(new Error('手机号码有误，请重填'));
                } else {
                    this.is_phone = true;
                    callback();
                }
            }
        };
        var validatePass5 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入验证码'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                username: '',
                password: '',
                phone: '',
                code: ''
            },
            rules: {
                username: [{ validator: validatePass, trigger: 'blur' }],
                password: [{ validator: validatePass2, trigger: 'blur' }],
                phone: [{ validator: validatePass3, trigger: 'blur' }],
                code: [{ validator: validatePass5, trigger: 'blur' }]
            },
            is_phone: false
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    Register({
                        username: this.ruleForm.username,
                        password: this.ruleForm.password,
                        phone: this.ruleForm.phone,
                        code: this.ruleForm.code
                    }).then(res => {
                        this.$message(res.data.msg);
                    });
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        get_code() {
            code({ params: { phone: this.ruleForm.phone } }).then(res => {
                if (res.data.code == 200) {
                    this.$message('验证码发送成功，请注意查收');
                } else {
                    this.$message(res.data.msg);
                }
            });
        },
        // 跳转登录
        login() {
            this.$router.push({ path: '/login' });
        }
    }
};
</script>

<style scoped>
.reg-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/盗梦空间.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-reg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.reg-btn {
    text-align: center;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>